<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | PHP version 5                                                        |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2004 The PHP Group                                |
// +----------------------------------------------------------------------+
// | This source file is subject to version 3.0 of the PHP license,       |
// | that is bundled with this package in the file LICENSE, and is        |
// | available through the world-wide-web at the following url:           |
// | http://www.php.net/license/3_0.txt.                                  |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// | license@php.net so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Authors: Original Author <author@example.com>                        |
// |          Your Name <you@example.com>                                 |
// +----------------------------------------------------------------------+
//
// $Id:$

if (!defined('APP_IN')) exit('Access Denied');
include ('page.php');
include (INC_DIR . 'api.func.php');
//当前模块
$mod_name = '车源api';
//允许操作
$ac_arr = array(
	'check' => 'token验证',
	'login' => '用户登录',
	'register' => '用户注册',
	'regjudge' => '注册验证',
	'modifypwd' => '修改密码',
    'search' => '查询车源',
    'cars' => '车源详情',
    'comparcode' => '对比车源信息',
	'assesscar' => '评估',
    'subcars' => '提交发布车源信息',
    'collectcars' => '车源收藏列表',
    'carmode' => '提交车源收藏',
	'delcollect' => '取消车源收藏',
	'demcode' => '提交经销商收藏',
    'mydealer' => '返回我的经销商收藏',
	'deldealercollect' => '取消经销商收藏',
	'dealercars'=>'二手车经销商车源',
	'dealertype' => '商家类型',
	'optionlist' => '搜索参数',
    'city' => '城市列表',
    'brand' => '一级品牌列表',
    'secondbrand' => '其他级品牌列表',
    'subass' => '提交评估信息',
    'upload' => '上传图片',
	'sellcars' => '置为已售出',
	'mypublishcars' => '我发布的车源列表',
    'updateversion' => '版本更新',
	'deletecars'=>'删除二手车',
	'indexad'=>'加载首页图片',
	'servicetel'=>'客服电话'
);

//当前操作
$ac = isset($_REQUEST['a']) && isset($ac_arr[$_REQUEST['a']]) ? $_REQUEST['a'] : '';

//加载首页广告获取token
if ($ac == "indexad") {
	$appid = isset($_GET['appid']) ? $_GET['appid'] : '';
	$appkey = isset($_GET['appkey']) ? $_GET['appkey'] : '';
    echo indexad($appid,$appkey);
}
else{
	$token = isset($_REQUEST['token']) ? trim($_REQUEST['token']) : '';
	$lastoken = urldecode($token);
	$lastoken = decrypt('phpstats',$lastoken);
	if(!empty($lastoken)){
		$array_token = explode("#",$lastoken);
		$tokendata = $db->row_select_one('token', "id=1");
		$isintoken = strstr($lastoken,$tokendata['token']);
		if(empty($isintoken)){
			$tokenlist = array('istoken'=>0);
			$jsonlist = json_encode($tokenlist);
			echo $jsonlist;
			exit;
		}
		//登录
		if($ac=="login"){
			$username = isset($_POST['username']) ? trim($_POST['username']) : '';
			$password = isset($_POST['password']) ? md5(trim($_POST['password'])) : '';

			if (!empty($username) and !empty($password))
			{
			   echo user_login($username,$password);
			}
		}
		//商家类型
		elseif($ac=="dealertype"){
			echo select_dealertype();
		}
		//判断用户名和手机号是否已注册过
		elseif($ac=="regjudge"){
			$username = $_GET['username'];
			$mobilephone = $_GET['mobilephone'];
			if(!empty($username)){
				echo user_register_username($username);
			}
			elseif(!empty($mobilephone)){
				echo user_register_mobilephone($mobilephone);
			}
		}
		//注册
		elseif($ac=="register"){
			$isdealer = isset($_POST['isdealer']) ? intval($_POST['isdealer']) : '';
			$username = isset($_POST['username']) ? trim($_POST['username']) : '';
			$password = isset($_POST['password']) ? md5(trim($_POST['password'])) : '';
			$nicname = isset($_POST['nicname']) ? trim($_POST['nicname']) : '';
			$mobilephone = isset($_POST['mobilephone']) ? trim($_POST['mobilephone']) : '';
			$shoptype = isset($_POST['shoptype']) ? intval($_POST['shoptype']) : '';
			$company = isset($_POST['company']) ? trim($_POST['company']) : '';
			if (!empty($username) and !empty($password) and !empty($mobilephone))
			{
			   echo user_register($username,$password,$nicname,$mobilephone,$shoptype,$company,$isdealer);
			}
		}
		//修改密码
		elseif($ac=="modifypwd"){
			$uid = $array_token[0];
			$oldpwd = isset($_POST['oldpwd']) ? trim($_POST['oldpwd']) : '';
			$newpwd = isset($_POST['newpwd']) ? md5(trim($_POST['newpwd'])) : '';
			if (!empty($uid) and !empty($oldpwd) and !empty($newpwd))
			{
			   echo user_modifypwd($uid,$oldpwd,$newpwd);
			}
		}
		//查询用户信息
		elseif($ac=="searchuser"){
			echo get_user($id);
		}
		//搜索参数
		elseif($ac=="optionlist"){
			echo select_option();
		}
		//车源列表
		elseif ($ac == "search") {
			$page = isset($_GET['page']) ? intval($_GET['page']) : 0;
			$status = isset($_GET['s']) ? intval($_GET['s']) : 0;
			$brand = isset($_GET['brand']) ? $_GET['brand'] : 0;;
			$subbrand = isset($_GET['subbrand']) ? intval($_GET['subbrand']) : 0;
			$price = isset($_GET['price']) ? intval($_GET['price']) : 0;
			$age = isset($_GET['age']) ? intval($_GET['age']) : 0;
			$kilometre = isset($_GET['kilometre']) ? intval($_GET['kilometre']) : 0;
			$transmission = isset($_GET['transmission']) ? intval($_GET['transmission']) : 0;
			$gas = isset($_GET['gas']) ? intval($_GET['gas']) : 0;
			$color = isset($_GET['color']) ? intval($_GET['color']) : 0;
			$source = isset($_GET['source']) ? intval($_GET['source']) : 0;
			$class = isset($_GET['class']) ? intval($_GET['class']) : 0;
			$picture = isset($_GET['picture']) ? intval($_GET['picture']) : 0;
			$keywords = isset($_GET['keywords']) ? $_GET['keywords'] : 0;;
			$mobilecode = isset($_REQUEST['mobilecode']) ? $_REQUEST['mobilecode'] : '';
			$identify = isset($_GET['identify']) ? intval($_GET['identify']) : 0;
			$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
			$tel = isset($_GET['tel']) ? intval($_GET['tel']) : 0;
			$code = isset($_REQUEST['code']) ? $_REQUEST['code'] : '';
			$aid=isset($_GET['aid']) ? intval($_GET['aid']) : 0;
			$cid=isset($_GET['cid']) ? intval($_GET['cid']) : 0;
			$uid=isset($_GET['uid']) ? intval($_GET['uid']) : 0;
			$sameprice = isset($_GET['sameprice']) ? intval($_GET['sameprice']) : 0;
			$samebrand = isset($_GET['samebrand']) ? intval($_GET['samebrand']) : 0;
			$comparcode = isset($_GET['comparcode']) ? intval($_GET['comparcode']) : 0;
			echo search_cars($status, $page, $brand, $subbrand, $price, $age, $kilometre, $transmission, $gas, $color, $source, $class, $picture, $aid, $cid, $uid, $keywords, $mobilecode, $identify, $tel, $code, $sameprice, $samebrand, $comparcode);
		}
		//车源收藏列表
		elseif ($ac == "collectcars") {
			$uid = isset($_GET['uid']) ? intval($_GET['uid']) : 0;
			$orderby = isset($_GET['s']) ? intval($_GET['s']) : 0;
			echo collect_cars($uid,$orderby);
		}
		//我发布的车源列表
		elseif ($ac == "mypublishcars") {
			$uid = isset($_GET['uid']) ? intval($_GET['uid']) : 0;
			$mobilecode = isset($_GET['mobilecode']) ? trim($_GET['mobilecode']) : 0;
			$orderby = isset($_GET['orderby']) ? intval($_GET['orderby']) : 0;
			echo my_publishcars($uid,$mobilecode,$orderby);
		}
		//车源详情
		elseif ($ac == "cars") {
			$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
			$uid = isset($_GET['uid']) ? intval($_GET['uid']) : 0;
			//$mobilecode = isset($_GET['mobilecode']) ? intval($_GET['mobilecode']) : 0;
			echo search_cars_detail($id,$uid);
		}
		//提交车源信息
		elseif ($ac == "subcars") {
			$uid = intval($array_token[0]);
			if(strlen($array_token[0])>10){
				$mobilecode = trim($array_token[0]);
				$uid = 0;
			}
			else{
				$mobilecode = "";
				$uid = intval($array_token[0]);
			}
			$carid = intval($array_token[2]);
			$cid = $_POST['cid'];
			$aid = $_POST['aid'];
			$brand = $_POST['brand'];
			$subbrand = $_POST['subbrand'];
			$picone = $_POST['picone'];
			$pictwo = $_POST['pictwo'];
			$picthree = $_POST['picthree'];
			$picfour = $_POST['picfour'];
			$picfive = $_POST['picfive'];
			$picsix = $_POST['picsix'];
			$color = $_POST['color'];
			$price = $_POST['price'];
			$transmission = $_POST['transmission'];
			$details = $_POST['details'];
			$uid = $_POST['uid'];
			$carname = $_POST['carname'];
			$model = $_POST['model'];
			$tel = $_POST['tel'];
			$allname = $_POST['allname'];
			$subsubbrand = $_POST['subsubbrand'];
			$sex = $_POST['sex'];
			$username = $_POST['username'];
			$ios = $_POST['ios'];
			$year = $_POST['year'];
			$month = $_POST['month'];
			$kilometre = $_POST['kilometre'];
			$transmission = $_POST['transmission'];
			$gas = $_POST['gas'];
			$emission = $_POST['emission'];
			echo submit_cars($brand,$subbrand,$subsubbrand,$allname,$model,$emission,$gas,$year,$month, $aid, $cid, $kilometre,$transmission,$color, $price, $details,$picone,$pictwo,$picthree,$picfour,$picfive,$picsix,$username,$tel,$mobilecode,$uid,$ios,$carid);
		} 
		//上传图片(ios)
		elseif ($ac == "upload") {
			$mobilecode = $_POST['mobilecode'];
			echo submit_upload($mobilecode);
		}
		//一级品牌列表
		elseif ($ac == "brand") {
			echo select_brands($brand);
		}
		//其他级品牌列表
		elseif ($ac == "secondbrand") {
			$brand = isset($_GET['brand']) ? $_GET['brand'] : 0;
			echo select_secondbrands($brand);
		}
		//城市列表
		elseif ($ac == "city") {
			echo select_city();
		}
		//返回我的咨询信息
		elseif ($ac == "consult") {
			$mobilecode = $_REQUEST['mobilecode'];
			$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
			echo select_consult($mobilecode);
		}
		//返回我的评估师信息
		elseif ($ac == "appraiser") {
			echo select_appraiser();
		}
		//返回车源对比信息
		elseif ($ac == "compcar") {
			$ids = isset($_GET['ids']) ? $_GET['ids'] : 0;
			echo select_compcar($ids);
		}
		//返回我的评估车源信息
		elseif ($ac == "asscars") {
			$mobilecode = isset($_GET['mobilecode']) ? intval($_GET['mobilecode']) : 0;
			$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
			echo select_ascars($id, $mobilecode);
		}
		//提交评估车源信息
		elseif ($ac == "subass") {
			$mobilecode = $_POST['mobilecode'];
			$brand = isset($_POST['brand']) ? intval($_POST['brand']) : 0;
			$subbrand = isset($_POST['subbrand']) ? intval($_POST['subbrand']) : 0;
			$subsubbrand = isset($_POST['subsubbrand']) ? intval($_POST['subsubbrand']) : 0;
			$aid = isset($_POST['aid']) ? intval($_POST['aid']) : 0;
			$cid = isset($_POST['cid']) ? intval($_POST['cid']) : 0;
			$bid = isset($_POST['bid']) ? intval($_POST['bid']) : 0;
			$kilometre = isset($_POST['kilometre']) ? intval($_POST['kilometre']) : 0;
			$allname = $_POST['allname'];
			$year = $_POST['year'];
			$month = $_POST['month'];
			$tel = $_POST['tel'];
			$details = $_POST['details'];
			$question = $_POST['question'];
			$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
			echo submit_assess($mobilecode, $brand, $subbrand, $subsubbrand, $allname, $year, $month, $kilometre, $tel, $details, $aid, $cid, $question, $id);
		}
		//提交车源收藏
		elseif ($ac == "carmode") {
			$uid = intval($array_token[0]);
			$carid = intval($array_token[2]);
			echo insert_mycarsmobilecode($uid,$carid);
		}
		//批量删除车源收藏
		elseif ($ac == "delcollect") {
			$uid = intval($array_token[0]);
			$carid = intval($array_token[2]);
			echo del_mycarsmobilecode($uid,$carid);
		}
		//提交经销商收藏
		elseif ($ac == "demcode") {
			$dealerid = intval($array_token[0]);
			$uid = intval($array_token[2]);
			echo insert_mydealermobilecode($uid,$dealerid);
		}
		//批量删除经销商收藏
		elseif ($ac == "deldealercollect") {
			$dealerid = intval($array_token[0]);
			$uid = intval($array_token[2]);
			echo del_mydealermobilecode($uid,$dealerid);
		}
		//返回我的经销商收藏
		elseif($ac=="mydealer"){
			 $uid = isset($_GET['uid']) ? intval($_GET['uid']) : 0;
			 echo select_dealer($uid);
		}
		//二手车经销商车源
		elseif($ac=="dealercars"){
			 $uid = isset($_GET['uid']) ? intval($_GET['uid']) : 0;
			 $status = isset($_GET['status']) ? intval($_GET['status']) : 0;
			 $page = isset($_GET['page']) ? intval($_GET['page']) : 0;
			 echo dealer_cars($uid,$status,$page);
		}
		//返回更新版本信息
		elseif ($ac == "updateversion") {
			$ios = isset($_GET['ios']) ? intval($_GET['ios']) : 0;
			echo update_version($ios);
		}
		//评估
		elseif($ac=="assesscar"){
			 $mk = isset($_GET['mk']) ? intval($_GET['mk']):0;        //跑了多少公里
			 $smk =isset($_GET['smk']) ? intval($_GET['smk']):30;     //车辆限制一共多少公里 一般为30万公里
			 $reyear= isset($_GET['reyear']) ? intval($_GET['reyear']):0;      //使用多少年
			 $remonth=isset($_GET['remonth']) ? intval($_GET['remonth']):0;    //使用多少月
			 $subsubbrand=isset($_GET['subsubbrand']) ? intval($_GET['subsubbrand']):0;
			 echo tel_rapidasscars($subsubbrand,$mk,$reyear,$remonth);
		}
		//删除二手车
		elseif($ac =='deletecars'){
			if(strlen($array_token[0])>10){
				$mobilecode = trim($array_token[0]);
				$uid = 0;
			}
			else{
				$mobilecode = "";
				$uid = intval($array_token[0]);
			}
			$carid = intval($array_token[2]);
			echo deletecars($carid,$uid,$mobilecode);
		}
		//置为已售出
		elseif ($ac == "sellcars") {
			if(strlen($array_token[0])>10){
				$mobilecode = trim($array_token[0]);
				$uid = 0;
			}
			else{
				$mobilecode = "";
				$uid = intval($array_token[0]);
			}
			$carid = isset($_POST['carid']) ? intval($_POST['carid']) : 0;
			$uid = isset($_POST['uid']) ? intval($_POST['uid']) : 0;
			echo sellcars($carid,$uid,$mobilecode);
		}
		//客服电话
		elseif ($ac == "servicetel") {
			$list = array(
				'tel' => array(
					'0'=>'023-67468745',
					'1'=>'023-67468745'
				)
			);
			$jsonlist = json_encode($list);
			echo $jsonlist;
		}
		else {
			exit;
		}
	}
	else{
		$tokenlist = array('istoken'=>0);
		$jsonlist = json_encode($tokenlist);
		echo $jsonlist;
		exit;
	}
}
?>
